package com.lin.filegraph.utils.parser.node;

import com.lin.filegraph.utils.calculator.CodeSizeCalculator;
import org.eclipse.jdt.core.dom.ASTNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 10:08
 */

public class JTreeNode {
    protected List<JTreeNode> childrenNodeList = new ArrayList<JTreeNode>();
    protected JTreeNode fatherNode = null;
    protected int startLine = -1;
    protected int endLine = -1;
    protected int LOC = -1;
    protected String filePath = null;
    protected ASTNode astNode = null;
    protected JProjectNode root = null;
    protected String relativePath = null;

    public JTreeNode(String filePath, ASTNode astNode) {
        super();
        this.filePath = filePath;
        this.relativePath = "";
        this.astNode = astNode;
        this.startLine = CodeSizeCalculator.calculateStartLine(astNode);
        this.endLine = CodeSizeCalculator.calculateEndLine(astNode);
        this.LOC = endLine - startLine + 1;
        if(this instanceof JProjectNode) {
            root = (JProjectNode) this;
        }
    }


    public List<? extends JTreeNode> getChildrenNodeList() {
        return childrenNodeList;
    }


    public JTreeNode getFatherNode() {
        return fatherNode;
    }

    private void setFatherNode(JTreeNode fatherNode) {
        this.fatherNode = fatherNode;
    }


    public int getStartLine() {
        return startLine;
    }


    public int getEndLine() {
        return endLine;
    }

    public int getLOC() {
        return LOC;
    }

    public String getFilePath() {
        return filePath;
    }

    public ASTNode getAstNode() {
        return astNode;
    }

    public String getRelativePath() {
        return relativePath;
    }

    private void setRelativePath(String relativePath) {
        this.relativePath = relativePath;
    }

    public void addChildNode(JTreeNode childNode) {
        childrenNodeList.add(childNode);
        childNode.setFatherNode(this);
        childNode.root = this.getRoot();
        String aPath = childNode.getFilePath();
        String projPath = childNode.root.getFilePath();
        String rPath = null;
        if(aPath.startsWith(projPath)) {
            if(aPath.equals(projPath)) {
                rPath = "";
            } else {
                rPath = aPath.substring(projPath.length() + 1);
            }
        } else {
            rPath = aPath;
            System.err.println("TreeNode.java-addChildNode-setRPath ERR");
        }
        childNode.setRelativePath(rPath);

    }

    public JProjectNode getRoot() {
        return root;
    }
}

